import * as task from "../../api/task";

Page({
    data: {
        currentPage:1,
        list:[],
        currentPage:1,
        hasNextPage:true,
        total:"",
        taskNo:"",
        taskName:""
    },
    onLoad(option){
        this.setData({
            taskNo:option.taskNo,
            taskName:option.taskName
        })
        this.get_order_list();
    },
    get_order_list(){

        task.increaseTaskProcessList({
            currentPage:this.data.currentPage,
            pageSize:10,
            taskNo:this.data.taskNo
        })
        .then((res)=>{

            this.setData({
                list:this.data.currentPage == 1 ? res.list :[...this.data.list, ...res.list],
                hasNextPage:res.hasNextPage,
                total:res.total
            })
        })
    },
    top_picker_confirm(e){

        this.setData({
            list:[],
            hasNextPage:true,
            total:"",
            currentPage:1,
            createTime:e.detail
        })

        this.get_order_list();
    },
    handle_order_detail(e){
        let { item } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/order-detail/order-detail?orderNo=' + item.orderNo
        });
    },
    onReachBottom(){
        if(this.data.hasNextPage){
            this.setData({
                currentPage: ++this.data.currentPage
            })
            this.get_order_list();
        }
    },
    // 保存图片
    saveImg: function (e) {

        let that = this;
        let imgSrc = e.currentTarget.dataset.item;

        wx.showLoading({
            title:"正在保存..."
        });
        //获取相册授权
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success() {
                            that.saveImgToLocal(imgSrc);
                        },
                        fail() {
                            wx.hideLoading();
                        }
                    })
                } else {//用户已经授权过了
                    that.saveImgToLocal(imgSrc);
                }
            }
        })

    },
    saveImgToLocal(imgSrc) {
        //var imgSrc = this.data.poster; //base64编码
        var save = wx.getFileSystemManager();
        var number = Math.random();

        wx.downloadFile({
            url: imgSrc,
            success: function (res) {
                //图片保存到本地
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function (data) {
                        wx.showToast({
                            title: '保存成功!',
                        })
                    },
                    fail: function (err) {
                        if (err.errMsg === "saveImageToPhotosAlbum:fail cancel") {
                            wx.showToast({
                                title: '保存失败!',
                                icon: 'none'
                            })
                        }
                    },
                })
            },
            fail(){
                wx.showToast({
                    title: '保存出错!',
                    icon: 'none'
                })
            }
        })
    }
})
